package protocol.JT905_2014;

import java.util.HashMap;

public class Handle905_2014JT81 implements IHandle905_2014JT {
    @Override
    public String handle(JT_905_2014Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x03:
                return handle8103(cmd);
            case 0x04:
                return handle8104(cmd);
            case 0x05:
                return handle8105(cmd);
            case 0x07:
                return handle8107(cmd);
            case 0x08:
                return handle8108(cmd);
            default:
                return "905:未匹配的命令字"+(cmd.readOrder()+","+cmd.readSubOrder());
        }
    }

    private String handle8108(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:下发终端升级包");
        builder.append("\r\n");
        int type = cmd.readInt(1);
        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"终端");
        list.put(12,"道路运输证IC读卡器");
        list.put(52,"卫星定位模块");
        builder.append("升级类型("+type+"):"+list.getOrDefault(type,"未定义"));
        builder.append("\r\n");
        builder.append("制造商ID:"+cmd.readBCD(5));
        builder.append("\r\n");
        builder.append("固件版本号:"+cmd.readString(cmd.readInt(1)));
        builder.append("\r\n");
        builder.append("升级数据包:"+cmd.readBCD(cmd.readDWord()));
        builder.append("\r\n");

        return builder.toString();
    }

    private String handle8107(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:查询终端属性");
        builder.append("\r\n");
        return builder.toString();
    }

    private String handle8105(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:终端控制");
        builder.append("\r\n");

        int order = cmd.readInt(1);
        switch (order){
            case 0x01:{
                builder.append("无线升级:");
                builder.append("\r\n");
                builder.append("    设备类型:"+cmd.readInt(1));
                builder.append("\r\n");
                builder.append("    厂商标识:"+cmd.readInt(1));
                builder.append("\r\n");
                builder.append("    硬件版本号:"+cmd.readBCD(1));
                builder.append("\r\n");
                builder.append("    软件版本号:"+cmd.readBCD(2));
                builder.append("\r\n");
                builder.append("    APN:"+cmd.readString(-1));
                builder.append("\r\n");
                builder.append("    拨号用户名:"+cmd.readString(-1));
                builder.append("\r\n");
                builder.append("    拨号密码:"+cmd.readString(-1));
                builder.append("\r\n");
                builder.append("    升级服务器地址:"+cmd.readString(-1));
                builder.append("\r\n");
                builder.append("    升级服务器端口:"+cmd.readInt(2));
                break;
            }
            case 0x02:{
                builder.append("终端关机");
                break;
            }
            case 0x03:{
                builder.append("终端复位");
                break;
            }
            case 0x04:{
                builder.append("终端恢复出厂设置");
                break;
            }
            case 0x05:{
                builder.append("关闭数据通信");
                break;
            }
            case 0x06:{
                builder.append("关闭所有无线通信");
                break;
            }
        }
        builder.append("\r\n");
        return builder.toString();
    }

    private String handle8104(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:查询终端参数");
        builder.append("\r\n");
        do {
            builder.append("参数ID:"+cmd.readBCD(2));
            builder.append("\r\n");
        }while (cmd.leftSize()>1);

        return builder.toString();
    }

    private String handle8103(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:设置终端参数");
        builder.append("\r\n");
        handleA10(cmd,builder);

        return builder.toString();
    }

    private void handleA10(JT_905_2014Cmd cmd, StringBuilder builder){
        do{
            int id = cmd.readInt(2);
            int len = cmd.readInt(1);
            builder.append("    "+Integer.toHexString(id)+"-");
            switch (id){
                case 0x0001:
                    builder.append("终端心跳发送间隔，秒:"+cmd.readInt(len));
                    break;
                case 0x0002:
                    builder.append("TCP消息应答超时时间，秒:"+cmd.readInt(len));
                    break;
                case 0x0003:
                    builder.append("TCP消息重传次数:"+cmd.readInt(len));
                    break;
                case 0x0004:
                    builder.append("SMS消息应答超时时间，秒:"+cmd.readInt(len));
                    break;
                case 0x0005:
                    builder.append("SMS消息重传次数:"+cmd.readInt(len));
                    break;
                case 0x0010:
                    builder.append("主服务器APN，无线通信拨号访问点:"+cmd.readString(len));
                    break;
                case 0x0011:
                    builder.append("主服务器无线通信拨号用户名:"+cmd.readString(len));
                    break;
                case 0x0012:
                    builder.append("主服务器无线通信拨号密码:"+cmd.readString(len));
                    break;
                case 0x0013:
                    builder.append("主服务器地址,IP或域名:"+cmd.readString(len));
                    break;
                case 0x0014:
                    builder.append("备份服务器APN，无线通信拨号访问点:"+cmd.readString(len));
                    break;
                case 0x0015:
                    builder.append("备份服务器无线通信拨号用户名:"+cmd.readString(len));
                    break;
                case 0x0016:
                    builder.append("备份服务器无线通信拨号密码:"+cmd.readString(len));
                    break;
                case 0x0017:
                    builder.append("备份服务器地址,IP或域名:"+cmd.readString(len));
                    break;
                case 0x0018:
                    builder.append("主服务器TCP端口:"+cmd.readInt(len));
                    break;
                case 0x0019:
                    builder.append("备份服务器TCP端口:"+cmd.readInt(len));
                    break;
                case 0x001A:
                    builder.append("一卡通主服务器地址,IP或域名（保留）:"+cmd.readString(len));
                    break;
                case 0x001B:
                    builder.append("一卡通主服务器TCP端口（保留）:"+cmd.readInt(len));
                    break;
                case 0x001C:
                    builder.append("一卡通备份服务器地址IP或域名（保留）:"+cmd.readString(len));
                    break;
                case 0x001D:
                    builder.append("一卡通备份服务器TCP端口（保留）:"+cmd.readInt(len));
                    break;
                case 0x0020:{
                    HashMap<Integer,String> list = new HashMap<>();
                    int type = cmd.readInt(len);
                    list.put(0,"定时汇报");
                    list.put(1,"定距汇报");
                    list.put(2,"定时 + 定距汇报");
                    builder.append("位置汇报策略:"+list.getOrDefault(type,"未定义"));
                    break;
                }
                case 0x0021:{
                    HashMap<Integer,String> list = new HashMap<>();
                    int type = cmd.readInt(len);
                    list.put(0,"根据ACC状态");
                    list.put(1,"根据空重车状态");
                    list.put(2,"根据登录状态 + ACC状态，先判断登录状态，若登录再根据ACC状态");
                    list.put(3,"根据登录状态 + 空重车状态，先判断登录状态，若登录再根据空重车状态");
                    builder.append("位置汇报方案:"+list.getOrDefault(type,"未定义"));
                    break;
                }
                case 0x0022:
                    builder.append("未登录汇报时间间隔，秒:"+cmd.readInt(len));
                    break;
                case 0x0023:
                    builder.append("ACC OFF汇报时间间隔，秒:"+cmd.readInt(len));
                    break;
                case 0x0024:
                    builder.append("ACC ON汇报时间间隔，秒:"+cmd.readInt(len));
                    break;
                case 0x0025:
                    builder.append("空车汇报时间间隔，秒:"+cmd.readInt(len));
                    break;
                case 0x0026:
                    builder.append("重车汇报时间间隔，秒:"+cmd.readInt(len));
                    break;
                case 0x0027:
                    builder.append("休眠时汇报时间间隔，秒:"+cmd.readInt(len));
                    break;
                case 0x0028:
                    builder.append("紧急报警时汇报时间间隔，秒:"+cmd.readInt(len));
                    break;
                case 0x0029:
                    builder.append("未登录汇报距离间隔，米:"+cmd.readInt(len));
                    break;
                case 0x002A:
                    builder.append("ACC OFF汇报距离间隔，米:"+cmd.readInt(len));
                    break;
                case 0x002B:
                    builder.append("ACC ON汇报距离间隔，米:"+cmd.readInt(len));
                    break;
                case 0x002C:
                    builder.append("空车汇报距离间隔，米:"+cmd.readInt(len));
                    break;
                case 0x002D:
                    builder.append("重车汇报距离间隔，米:"+cmd.readInt(len));
                    break;
                case 0x002E:
                    builder.append("休眠时汇报距离间隔，米:"+cmd.readInt(len));
                    break;
                case 0x002F:
                    builder.append("紧急报警时汇报距离间隔，米:"+cmd.readInt(len));
                    break;
                case 0x0030:
                    builder.append("拐点补传角度:"+cmd.readInt(len));
                    break;
                case 0x0040:
                    builder.append("监控指挥中心电话号码:"+cmd.readString(len));
                    break;
                case 0x0041:
                    builder.append("复位电话号码:"+cmd.readString(len));
                    break;
                case 0x0042:
                    builder.append("恢复出厂设置电话号码:"+cmd.readInt(len));
                    break;
                case 0x0043:
                    builder.append("监控指挥中心SMS电话号码:"+cmd.readInt(len));
                    break;
                case 0x0044:
                    builder.append("接收终端SMS文本报警号码:"+cmd.readInt(len));
                    break;
                case 0x0045:{
                    HashMap<Integer,String> list = new HashMap<>();
                    int type = cmd.readInt(len);
                    list.put(0,"自动接听");
                    list.put(1,"ACC ON时自动接听，OFF时手动接听");
                    builder.append("终端电话接听策略:"+list.getOrDefault(type,"未定义"));
                    break;
                }
                case 0x0046:
                    builder.append("每次最长通话时间，秒:"+cmd.readInt(len));
                    break;
                case 0x0047:
                    builder.append("当月最长通话时间，秒:"+cmd.readInt(len));
                    break;
                case 0x0048:
                    builder.append("电话短号长度:"+cmd.readInt(len));
                    break;
                case 0x0049:
                    builder.append("监听电话号码:"+cmd.readString(len));
                    break;
                case 0x004A:
                    builder.append("终端设备维护密码:"+cmd.readString(len));
                    break;
                case 0x004B:
                    builder.append("终端的语音播报音量控制0-9 0静音 9为最高:"+cmd.readInt(len));
                    break;
                case 0x0050:
                    builder.append("报警屏蔽字，与位置信息汇报消息中的报警标志相对应，相应位为1则相应报警被屏蔽:"+cmd.readBCD(len));
                    break;
                case 0x0051:
                    builder.append("报警发送文本SMS开关，与位置信息汇报消息中的报警标志相对应，相应位为1则相应报警时发送文本SMS:"+cmd.readBCD(len));
                    break;
                case 0x0052:
                    builder.append("报警拍摄开关，与位置信息汇报消息中的报警标志相对应，相应位为1则相应报警时摄像头拍摄:"+cmd.readBCD(len));
                    break;
                case 0x0053:
                    builder.append("报警拍摄存储标志，与位置信息汇报消息中的报警标志相对应，相应位为1则对相应报警时拍的照片进行存储，否则实时上传:"+cmd.readBCD(len));
                    break;
                case 0x0055:
                    builder.append("最高速度，KM/H:"+cmd.readInt(len));
                    break;
                case 0x0056:
                    builder.append("超速持续时间，秒:"+cmd.readInt(len));
                    break;
                case 0x0057:
                    builder.append("连续驾驶时间门限，秒:"+cmd.readInt(len));
                    break;
                case 0x0058:
                    builder.append("最小休息时间，秒:"+cmd.readInt(len));
                    break;
                case 0x0059:
                    builder.append("最长停车时间，秒:"+cmd.readInt(len));
                    break;
                case 0x005A:
                    builder.append("当天累计驾驶时间门限，秒:"+cmd.readInt(len));
                    break;
                case 0x0070:
                    builder.append("图像/视频质量，1～10，1最好:"+cmd.readInt(len));
                    break;
                case 0x0071:
                    builder.append("亮度:"+cmd.readInt(len));
                    break;
                case 0x0072:
                    builder.append("对比度:"+cmd.readInt(len));
                    break;
                case 0x0073:
                    builder.append("饱和度:"+cmd.readInt(len));
                    break;
                case 0x0074:
                    builder.append("色度:"+cmd.readInt(len));
                    break;
                case 0x0080:
                    builder.append("车辆里程表读数，1/10KM:"+cmd.readInt(len));
                    break;
                case 0x0081:
                    builder.append("车辆所在的省域ID，1～255（省级行政区划代码前2位；终端根据此ID生成所在省的简称即对应车牌号第一个汉字）:"+cmd.readInt(len));
                    break;
                case 0x0082:
                    builder.append("车辆所在的市域ID，1～255（行政区划代码的第3、4位）:"+cmd.readInt(len));
                    break;
                case 0x0090:
                    builder.append("计程计价装置营运次数限制，0-9999；0表示不做限制:"+cmd.readBCD(len));
                    break;
                case 0x0091:
                    builder.append("计程计价装置营运时间限制，YYYY-MM-DD-hh，0000000000表示不做限制:"+cmd.readBCD(len));
                    break;
                case 0x0092:
                    builder.append("企业编码:"+cmd.readString(len));
                    break;
                case 0x0093:
                    builder.append("企业简称:"+cmd.readString(len));
                    break;
                case 0x0094:
                    builder.append("车牌号码:"+cmd.readString(len));
                    break;
                case 0x00A0:
                    builder.append("智能终端录音模式（0X01：全程录音；0X02：翻牌录音）:"+cmd.readInt(len));
                    break;
                case 0x00A1:
                    builder.append("智能终端录音文件最大时长（1-255）分钟:"+cmd.readInt(len));
                    break;
                case 0x00A2:
                    builder.append("液晶（终端）心跳时间间隔(单位S 1-60):"+cmd.readInt(len));
                    break;
                case 0x00A3:
                    builder.append("LED心跳时间间隔(单位S 1-60):"+cmd.readInt(len));
                    break;
                case 0x00AF:
                    builder.append("ACC OFF后进入休眠模式的时间，单位S:"+cmd.readInt(len));
                    break;
                case 0x00B0:
                    builder.append("视频服务器协议模式 0X00：TCP；0X01：UDP（保留）:"+cmd.readInt(len));
                    break;
                case 0x00B1:
                    builder.append("视频服务器APN，无线通信拨号访问点（保留）:"+cmd.readString(len));
                    break;
                case 0x00B2:
                    builder.append("视频服务器无线通信拨号用户名（保留）:"+cmd.readString(len));
                    break;
                case 0x00B3:
                    builder.append("视频服务器无线通信拨号密码（保留）:"+cmd.readString(len));
                    break;
                case 0x00B4:
                    builder.append("视频服务器地址,IP或域名（保留）:"+cmd.readInt(len));
                    break;
                case 0x00B5:
                    builder.append("视频服务器端口（保留）:"+cmd.readString(len));
                    break;
                default:
                    builder.append("未定义ID("+id+"):"+cmd.readBCD(len));
                    break;
            }

            builder.append("\r\n");
        }while (cmd.leftSize() > 3);
    }

}
